import Cookies from 'js-cookie'
import { appName } from '@/setting'
// 所有的cookie都加统一的前缀，前缀来至于vue项目配置，这样可以避免同域名下cookie污染
const cookies = {
    /**
    * @description 存储 cookie 值
    * @param {String} name cookie name
    * @param {String} value cookie value
    * @param {Object} setting cookie setting
    */
    set: function (name: string = 'default', value: string = '', cookieSetting: any = {}) {
        const currentCookieSetting = {
            expires: 1
        }
        Object.assign(currentCookieSetting, cookieSetting)
        Cookies.set(`${appName}-${name}`, value, currentCookieSetting)
    },
    /**
     * @description 拿到 cookie 值
     * @param {String} name cookie name
     */
    get: function (name: string = 'default') {
        return Cookies.get(`${appName}-${name}`)
    },
    /**
     * @description 拿到 cookie 全部的值
     */
    getAll: function () {
        return Cookies.get()
    },
    /**
     * @description 删除 cookie
     * @param {String} name cookie name
     */
    remove: function (name: string = 'default') {
        return Cookies.remove(`${appName}-${name}`)
    }
}

export default cookies